package netty.client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class SimpleHandler extends MessageToMessageDecoder<String> {

    public SimpleHandler() {
        log.info(">>> init");
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, String msg, List<Object> out) throws Exception {
        log.info("receive msg: {}", msg.trim());
        log.info("receive out: {}", out);

        if ("quit".equals(msg.trim())) {
            ctx.close();
            return;
        }

        ctx.channel().writeAndFlush(new StringBuffer(msg.trim()).reverse().append("\n").toString());
    }
}
